package yns.springboot.guide.demo.task;

import java.util.Random;
import java.util.concurrent.CompletableFuture;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

@EnableAsync
@Component
public class Task {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	public static Random random = new Random();

	@Async("taskExecutor")
	public CompletableFuture<String> doTaskOne() throws Exception {
		logger.info("开始做任务一");
		long start = System.currentTimeMillis();
		Thread.sleep(random.nextInt(10000));
		long end = System.currentTimeMillis();
		logger.info("完成任务一，耗时：" + (end - start) + "毫秒");
		return CompletableFuture.completedFuture("任务一完成");
	}

	@Async("taskExecutor")
	public CompletableFuture<String> doTaskTwo() throws Exception {
		logger.info("开始做任务二");
		long start = System.currentTimeMillis();
		Thread.sleep(random.nextInt(10000));
		long end = System.currentTimeMillis();
		logger.info("完成任务二，耗时：" + (end - start) + "毫秒");
		return CompletableFuture.completedFuture("任务二完成");
	}

	@Async("taskExecutor")
	public CompletableFuture<String> doTaskThree() throws Exception {
		logger.info("开始做任务三");
		long start = System.currentTimeMillis();
		Thread.sleep(random.nextInt(10000));
		long end = System.currentTimeMillis();
		logger.info("完成任务三，耗时：" + (end - start) + "毫秒");
		return CompletableFuture.completedFuture("任务三完成");
	}
}
